var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
Page({
  data: {
    imgUrl: globalData.imgUrl,
    titleFlag: "全部",
    titleList: ["全部", "未使用", "已过期", "已使用"],//状态 1,未核销/2,已核销/3已过期
    pageNumber: 1,
    pageSize: 10,
    totalPage: 1,//总页数
    list: [],
  },
  list: function () {
    var params = new Object();
    params.pageNumber = this.data.pageNumber;
    params.pageSize = this.data.pageSize;
    let titleFlag = this.data.titleFlag;
    switch (titleFlag) {
      case '未使用': params.status = 1; break;
      case '已使用': params.status = 2; break;
      case '已过期': params.status = 3; break;
    }
    var that = this;
    let openId = wx.getStorageSync("openidKey");
    console.log("param:" + JSON.stringify(params));
    wx.request({
      header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/member/myCoupon",
      dataType: "json",
      method: "GET",
      data: params,
      success: function (res) {
        res = res.data;
        console.log("res：" + JSON.stringify(res));
        if (res.result == 0) {
          let rows = res.data.list;
          if (rows != null && rows != "") {
            for (let i = 0; i < rows.length; i++) {
              if (rows[i].limitDescribe.length > 12) {
                rows[i].limitDescribestr = rows[i].limitDescribe.substring(0, 12) + "...";
              }
            }
          }
          that.setData({ totalPage: res.data.totalPage })
          if (that.data.pageNumber == 1) {
            that.setData({ list: rows })
          } else {
            var list = that.data.list;
            list = list.concat(rows);
            that.setData({ list: list })
          }
        }
      }
    })
  },
  //设置显示块id
  titleSelect: function (e) {
    let id = e.currentTarget.id;
    this.setData({
      titleFlag: id,
      page: 1,
    })
    this.list();
  },
  info: function (e) {
    let id = e.currentTarget.id;
    let list = this.data.list;
    let row = list[id];
    wx.setStorageSync("voucherInfo", row);
    wx.navigateTo({
      url: '../voucherInfo/voucherInfo',
    })
  },
  share: function (e) {
    let userCouponId = e.currentTarget.id;
    wx.navigateTo({
      url: '../../homepage/voucherShare/voucherShare?userCouponId=' + userCouponId,
    })
  },
  onLoad: function (options) {
    this.list();
  },
  onShow: function () {

  },
  onReachBottom: function () {
    let pageNumber = this.data.pageNumber;
    pageNumber = pageNumber + 1;
    if (pageNumber > this.data.totalPage) {
      commonUtil.showSuccessToast("没有更多啦~");
      return;
    } else {
      this.setData({ pageNumber: pageNumber })
      this.list();
    }
  },
})